/**
 * FIFS - The Find It File System
 * Copyright (C) 2006 Chris Oklota (chris at oklota dot net)
 * 
 * This program is distributed under the terms of the GNU LGPL.
 * See the file http://www.gnu.org/licenses/lgpl.txt
 */
package fifs.plugin;

import java.util.Set;

import org.apache.log4j.Logger;

import fifs.Fifs;
import fifs.FifsFile;
import fuse.FuseException;

/**
 * Base class for FIFS plugins
 */
public abstract class FifsPlugin implements Runnable {

    /** The logger for this class */
    public static final Logger log = Logger.getLogger(FifsPlugin.class);
    
    protected String path;
    protected FifsFile file;
    protected Fifs fs;
    
    public void setFilePath(String absolutePathToFile) {
        this.path = absolutePathToFile;
    }
    
    public void setFile(FifsFile fileToTag) {
        this.file = fileToTag;
    }
    
    public void setFilesystem(Fifs fifs) {
        this.fs = fifs;
    }
    
    public abstract Set<String> getTags();
    
    public void run() {
        if (log.isTraceEnabled()) {
            log.trace("Running FIFS plugin on file " + path);
        }
        
        try {

            Set<String> tags = getTags();
            if ((tags == null) || (tags.size() == 0)) {
                return;
            }

            // make sure all of the tags are in the filesystem
            fs.addTags(tags);

            // add the tags to the file
            file.getTags().addAll(tags);

            // update the file tags in the database
            if (!fs.getDao().updateFileTags(file)) {
                FuseException fe = new FuseException("I/O Error").initErrno(FuseException.EIO);
                if (log.isDebugEnabled()) {
                    log.debug("I/O Error", fe);
                }
                throw fe;
            }

        } catch (FuseException fe) {
            // nothing we can do here, exit the plugin thread
        }
    }
    
}
